<template>
  <div class="app-container" v-if="showDetail">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商户名称" prop="shopId">
        <el-select v-model="queryParams.shopId" placeholder="请选择商户名称" clearable>
          <el-option
            v-for="dict in shopList"
            :key="dict.id"
            :label="dict.businessName"
            :value="parseInt(dict.id)">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="商户地址" prop="shopId">
        <el-select v-model="queryParams.shopId" placeholder="请选择商户地址" clearable>
          <el-option
            v-for="dict in shopList"
            :key="dict.id"
            :label="dict.address"
            :value="parseInt(dict.id)">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['activity:activity:add']"
        >新增</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['activity:activity:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['activity:activity:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="activityList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="商户名称" align="center" prop="shopName" />
      <el-table-column label="商户地址" align="center" prop="shopAddress" />
      <el-table-column label="发布内容" align="center" prop="content" />
      <el-table-column label="发布时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="上/下架状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.che_activity_state" :value="scope.row.state"/>
        </template>
      </el-table-column>

      <el-table-column label="审核状态" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.che_activity_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="handleDetail(scope.row)"
          >详情</el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['activity:activity:edit']"
          >修改</el-button>

          <el-button
            size="mini"
            type="text"
            @click="upDown(scope.row.state,scope.row.id)"
          >{{scope.row.state ? "上架" : "下架"}}</el-button>

          <el-button
            size="mini"
            type="text"
            @click="review(scope.row.type,scope.row.id)"
          >{{scope.row.type ? "" : "审核"}}</el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改信息发布对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题">
          <editor v-model="form.title" :min-height="192"/>
        </el-form-item>
        <el-form-item label="商户名称" prop="shopId">

          <el-select v-model="form.shopId" placeholder="请选择商户名称" clearable>
            <el-option
              v-for="dict in shopList"
              :key="dict.id"
              :label="dict.businessName"
              :value="parseInt(dict.id)">
            </el-option>
          </el-select>

        </el-form-item>
        <el-form-item label="发布内容">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
<!--        <el-form-item label="审核状态" prop="type">-->
<!--          <el-radio-group v-model="form.type">-->
<!--            <el-radio-->
<!--              v-for="dict in dict.type.che_activity_type"-->
<!--              :key="dict.value"-->
<!--              :label="parseInt(dict.value)"-->
<!--            >{{dict.label}}</el-radio>-->
<!--          </el-radio-group>-->
<!--        </el-form-item>-->
        <el-form-item label="审核时间" prop="auditTime">
          <el-date-picker clearable
            v-model="form.auditTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择审核时间">
          </el-date-picker>
        </el-form-item>
<!--        <el-form-item label="审核说明" prop="explains">-->
<!--          <el-input v-model="form.explains" type="textarea" placeholder="请输入内容" />-->
<!--        </el-form-item>-->
        <el-form-item label="活动状态" prop="state">
          <el-radio-group v-model="form.state">
            <el-radio
              v-for="dict in dict.type.che_activity_state"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="reviewOpen" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="审核状态" prop="type">
          <el-radio-group v-model="form.type">
            <el-radio
              v-for="dict in dict.type.che_activity_types"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="审核说明" prop="explains">
          <el-input v-model="form.explains" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="sure">确 定</el-button>
        <el-button @click="quxiao">取 消</el-button>
      </div>
    </el-dialog>

  </div>

  <div class="detail" v-else>
    <div class="detail-1">
      <div class="de-left" >详情</div>

      <button class="de-right" @click="goBack">返回</button>
    </div>
<!--    表格-->
    <table class="resizable-table">
      <tr>
        <td style="background-color: #eee">标题：</td>
        <td>{{activity.title }}</td>
      </tr>
      <tr>
        <td style="background-color: #eee">发布人：</td>
        <td>{{shop.businessName }}</td>
      </tr>
      <tr>
        <td style="background-color: #eee">发布时间：</td>
        <td>{{activity.createTime }}</td>
      </tr>
      <tr>
        <td style="background-color: #eee">内容：</td>
        <td>{{activity.content }}</td>
      </tr>
      <tr>
<!--        <td style="background-color: #eee">图片：</td>-->
<!--        <td> {{shop.shopImage}}<image-preview src="shop.shopImage"  :width="200" :height="150"/>-->

<!--           <template slot-scope="{ row }">-->
<!--            < img :src="shop.shopImage" />-->
<!--            </template>-->

<!--          </td>-->
      </tr>
    </table>
  </div>
</template>

<script>
import { listActivity, getActivity, delActivity, addActivity, updateActivity } from "@/api/activity/activity";
import {listShop,getShop} from "@/api/shop/shop";

export default {
  name: "Activity",
  dicts: ['che_activity_state', 'che_activity_type','che_activity_types'],
  data() {
    return {
      id:'',
      shopId:'',
      // 遮罩层
      loading: true,
      // 商户名称
      shopList: [],
      // 默认显示页面
      showDetail:true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 信息发布表格数据
      activityList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        shopId: null
      },
      activity:{},
      shop:{},
      // 表单参数
      form: {},
      state: '',
      updata:{
        id : "",
        state: ""
      },
      reviewOpen:false,
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 审核弹出框
    review(type,row) {
      this.reset();
      const id = row
      getActivity(id).then(response => {
        this.form = response.data;
        this.reviewOpen = true;
        this.title = "审核";
      });
    },
    sure(){
      updateActivity(this.form).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.reviewOpen = false;
        this.getList();
      });
    },


    quxiao(){
      this.reviewOpen = false;
      this.reset();
      this.getList();
    },
    // 上下架
    upDown(state, id){
      let newState = state ? 0 : 1; // 切换状态
      this.updata = {
        state: newState,
        id: id
      };
      updateActivity(this.updata).then(response =>{
        // this.activityList = response.data
        this.$modal.msgSuccess("修改成功")
        this.getList();
      })

    },
    goBack(){
      this.showDetail=true;
    },
    shopInfo(){
      getShop(this.shopId).then(response =>{
        this.shop = response.data
      })
    },
    handleDetail(row){
      this.showDetail=false;
      this.id = row.id
      this.shopId = row.shopId
      getActivity(this.id).then(response =>{
        this.activity = response.data
      })
      this.shopInfo()
    },
    /** 查询信息发布列表 */
    getList() {
      this.loading = true;
      listActivity(this.queryParams).then(response => {
        this.activityList = response.rows;
        this.total = response.total;
        this.loading = false;
      });

      listShop().then(response => {
        this.shopList = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        shopName: null,
        content: null,
        createTime: null,
        type: null,
        auditTime: null,
        explains: null,
        state: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加信息发布";
      this.form  = {
        type: 0,
        state: 0
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getActivity(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改信息发布";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateActivity(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addActivity(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();

            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除信息发布编号为"' + ids + '"的数据项？').then(function() {
        return delActivity(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('activity/activity/export', {
        ...this.queryParams
      }, `activity_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style>
  .detail{
    width: 1050px;
    height: 450px;
    border: 2px solid #eee;
    margin: 0 auto;
    margin-top: 20px;
    /*opacity: 0.5;*/
  }
  .detail-1{
    display: flex;
    justify-content: space-between;
    margin: 10px 20px;
    border-bottom: 1px solid #eee;
    padding-bottom: 10px;
    box-sizing: border-box;
  }
  .de-right{
    width: 80px;
    height: 30px;
    background-color: #00afff;
    color: #FFFFFF;
    border: 1px solid #FFFFFF;
    border-radius: 6px;
    /*line-height: 30px;*/
  }
  .resizable-table {
    width: 1000px;
    margin: 0 auto;
    border-collapse: collapse; /* 合并边框 */
  }

  .resizable-table td {
    border: 1px solid #ddd; /* 表格单元格边框 */
    padding: 8px; /* 单元格内边距 */
    word-wrap: break-word; /* 如果内容太长，允许在单词内换行 */
    text-align: center;
  }
</style>
